function promiseAll(promises) {
  return new Promise(function(resolve, reject) {
    if(!Array.isArray(promises)){
        throw new TypeError(`请传入一个数组`)
    }
    let resolvedCounter = 0;
    const promiseNum = promises.length;
    const resolvedResult = [];
    for (let i = 0; i < promiseNum; i++) {
      //Promise.resolve方法会将这个对象转为 Promise 对象，然后就立即执行thenable对象的then方法。
      //如果参数是 Promise 实例，那么Promise.resolve将不做任何修改、原封不动地返回这个实例。
      Promise.resolve(promises[i]).then(value=>{
        resolvedCounter++;
        resolvedResult[i] = value;
        if (resolvedCounter === promiseNum) {
            return resolve(resolvedResult)
        }
      },error=>{
        return reject(error)
      })
    }
  })
}

// test
let p1 = new Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(1)
    }, 1000)
})
let p2 = new Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(2)
    }, 2000)
})
let p3 = new Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(3)
    }, 3000)
})
promiseAll([p3, p1, p2]).then(res => {
    console.log(res) // [3, 1, 2]
})
